package com.mark.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mark.entity.MyTableData;
import com.mark.entity.school.UTeacher;
import com.mark.entity.school.UTeacherGroup;
import com.mark.service.UTeacherService;
import com.mark.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * (UTeacher)表控制层
 *
 * @author Deng PengFei
 * @since 2021-10-27 11:17:56
 */
@CrossOrigin
@RestController
@RequestMapping("teacher")
public class UTeacherController {

    @Autowired
    private UTeacherService uTeacherService;


    /**
     * 增加一个教师
     *
     * @param uTeacher 教师实例
     * @return 1001 成功 、  1002 教师已存在 、 1003 失败
     */
    @PostMapping("addTeaSava")
    public Integer addNewTea(UTeacher uTeacher, UTeacherGroup uTeacherGroup) {
        System.out.println(uTeacher);
        System.out.println(uTeacherGroup);
        return this.uTeacherService.addNewTea(uTeacher, uTeacherGroup);
    }


    /**
     * 批量导入教师
     *
     * @param file 导入的excl文件
     */
    @PostMapping("importTeaList")
    public void importTeaList(MultipartFile file) {
        String fileName = file.getOriginalFilename(); // 获取文件名
        String savePath = ClassUtils.getDefaultClassLoader().getResource("").getPath(); // 获取保存路径
        try {
            File saveFile = new File(savePath + "/static/importFile/" + fileName);
            if (saveFile.exists()) {
                saveFile.delete();
            }
            file.transferTo(saveFile); //保存文件
            ArrayList<ArrayList<String>> excelList = FileUtil.readExcel(saveFile);
            this.uTeacherService.importTeaList(excelList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据分组Id获取教师组的信息
     *
     * @param groId 分组Id
     * @return 对象列表
     */
    @GetMapping("getTeaListByGroId")
    public List<UTeacher> getTeaListByGroId(Integer groId) {
        return this.uTeacherService.getTeaListByGroId(groId);
    }

    /**
     * 获取所有教师列表
     *
     * @return 对象列表
     */
    @GetMapping("getTeaList")
    public MyTableData getTeaList(Integer pageNum, Integer pageSize, String query) {
        PageHelper.startPage(pageNum, pageSize);
        List<UTeacher> teachers = this.uTeacherService.getTeaList(query);
        PageInfo<UTeacher> myPageInfo = new PageInfo<>(teachers);
        return new MyTableData(myPageInfo.getTotal(), teachers, "", 200);
    }

    /**
     * 根据教师Id删除教师信息
     *
     * @param teaId 教师Id
     * @return true 成功，false 失败
     */
    @GetMapping("delTeaByTeaId")
    public Boolean delTeaByTeaId(Integer teaId) {

        return false;
    }


}